//给定一棵树的前序遍历 preorder 与中序遍历 inorder。请构造二叉树并返回其根节点。 
//
// 
//
// 示例 1: 
//
// 
//Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
//Output: [3,9,20,null,null,15,7]
// 
//
// 示例 2: 
//
// 
//Input: preorder = [-1], inorder = [-1]
//Output: [-1]
// 
//
// 
//
// 提示: 
//
// 
// 1 <= preorder.length <= 3000 
// inorder.length == preorder.length 
// -3000 <= preorder[i], inorder[i] <= 3000 
// preorder 和 inorder 均无重复元素 
// inorder 均出现在 preorder 
// preorder 保证为二叉树的前序遍历序列 
// inorder 保证为二叉树的中序遍历序列 
// 
// Related Topics 树 数组 哈希表 分治 二叉树 👍 1349 👎 0

package leetcode.editor.cn;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class _105_ConstructBinaryTreeFromPreorderAndInorderTraversal {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public static void main(String[] args) {
        int[] preOrder = {3, 9, 20, 15, 7};
        int[] inOrder = {9, 3, 15, 20, 7};
//        int[] preOrder = {1,2};
//        int[] inOrder = {2,1};
        Solution solution = new _105_ConstructBinaryTreeFromPreorderAndInorderTraversal().new Solution();
        TreeNode res = solution.buildTree(preOrder, inOrder);
        System.out.println(res);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            return method_2(preorder, inorder);

            // 自己写的递归
//            return method_1(preorder, inorder);
        }

        private TreeNode method_2(int[] preorder, int[] inorder) {
            int preLen = preorder.length;
            int inLen = inorder.length;

            if (preLen != inLen) {
                throw new RuntimeException("Incorrect input data.");
            }

            Map<Integer, Integer> map = new HashMap<Integer, Integer>(inLen);
            for (int i = 0; i < inLen; i++) {
                map.put(inorder[i], i);
            }
            return method_2(preorder, 0, preLen - 1, map, 0, inLen - 1);
        }

        private TreeNode method_2(int[] preorder, int preLeft, int preRight,
                                  Map<Integer, Integer> map, int inLeft, int inRight) {

            if (preLeft > preRight || inLeft > inRight) {
                return null;
            }

            int rootVal = preorder[preLeft];
            TreeNode root = new TreeNode(rootVal);
            int pIndex = map.get(rootVal);

            root.left = method_2(preorder, preLeft + 1, pIndex - inLeft + preLeft,
                    map, inLeft, pIndex - 1);
            root.right = method_2(preorder, pIndex - inLeft + preLeft + 1, preRight,
                    map, pIndex + 1, inRight);
            return root;
        }

        /**
         * 自己写的递归，还有很多可以优化的地方，比如刚开始找index的地方，还有刚开始列出来下标关系会更容易
         * @param preorder
         * @param inorder
         * @return
         */
        private TreeNode method_1(int[] preorder, int[] inorder) {
            TreeNode root = new TreeNode(preorder[0]);
            int index = -1;
            for (int i = 0; i < inorder.length; i++) {
                if (inorder[i] == preorder[0]) {
                    index = i;
                }
            }
//            System.out.println(index);
            if ((inorder.length > 1) && (index != 0 || index != inorder.length - 1)) {
                int[] leftIn = Arrays.copyOfRange(inorder, 0, index);
                int[] leftPre = Arrays.copyOfRange(preorder, 1, index + 1);
                int[] rightPre = Arrays.copyOfRange(preorder, index + 1, preorder.length);
                int[] rightIn = Arrays.copyOfRange(inorder, index + 1, inorder.length);
//                System.out.println();
                if (index != 0) root.left = method_1(leftPre, leftIn);
                if (index != inorder.length - 1) root.right = method_1(rightPre, rightIn);
            }
            return root;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}